package com.ls.controller;

/**
 * @author atom.hu
 * @version V1.0
 * @Package com.ls.controller
 * @date 2021/10/20 16:32
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ls.common.BaseContext;
import com.ls.common.R;
import com.ls.dto.DishDto;
import com.ls.dto.OrdersDto;
import com.ls.entity.Dish;
import com.ls.entity.OrderDetail;
import com.ls.entity.Orders;
import com.ls.service.OrderDetailService;
import com.ls.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name,@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
            Date beginTime,@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime){
        //构造分页构造器对象
        Page<Orders> pageInfo = new Page<>(page,pageSize);
//        Page<OrdersDto> ordersDtoPage = new Page<>();
        //条件构造器
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(name != null,"orders.name",name);

        //3 设置查询条件
//        queryWrapper.like(number!=null, Orders::getNumber,number);
//        queryWrapper.ge(beginTime!=null, Orders::getOrderTime,beginTime);
//        queryWrapper.le(endTime!=null, Orders::getOrderTime,endTime);
        //时间查询
        //queryWrapper.between("order_time",beginTime,endTime);
        //按下单时间降序排序
        queryWrapper.lambda().orderByDesc(Orders::getOrderTime);
        orderService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
//        //拷贝
//        BeanUtils.copyProperties(pageInfo,ordersDtoPage,"records");
//        List<Orders> ordersList = pageInfo.getRecords();
//        ordersList = ordersList.stream().map((item)->{
//            OrdersDto ordersDto = new OrdersDto();
//            BeanUtils.copyProperties(item,ordersDto);
//            return item;
//
//        }).collect(Collectors.toList());
//        return null;
    }
    /**
     * 派送
     */
    @PutMapping
    public R<String> updateStatus(@RequestBody Orders orders){
        log.info("orders:{}",orders);
        Long ordersId = orders.getId();
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Orders::getId,ordersId);
        Orders one = orderService.getOne(queryWrapper);
        one.setStatus(orders.getStatus());
        orderService.updateById(one);
        return R.success("订单正在派送中");
    }
    /**
     * 最新订单
     */
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> userPage(int page, int pageSize){
        log.info("查询用户的订单：page={},pageSize={}",page,pageSize);
        //获取登陆用户的id
        Long userId = BaseContext.getCurrentId();
        //创建分页对象
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();
        //创建条件对象，设置查询条件
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId,userId);
        queryWrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(pageInfo, queryWrapper);
        BeanUtils.copyProperties(pageInfo,ordersDtoPage,"records");
        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> collect = records.stream().map((item) -> {
            OrdersDto ordersDto = new OrdersDto();
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(item, ordersDto);
            String number = item.getNumber();
            LambdaQueryWrapper<OrderDetail> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(OrderDetail::getOrderId, number);
//            orderDetailService.getById(number)
            List<OrderDetail> list = orderDetailService.list(queryWrapper1);
            ordersDto.setOrderDetails(list);
            return ordersDto;
        }).collect(Collectors.toList());
        ordersDtoPage.setRecords(collect);
        return R.success(ordersDtoPage);
    }
}
